package com.qding.park.common.core.validate;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.GenericValidator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;

public class RchlValidateTools {

	public static void validate(RchlValidateJsonBean cvb) throws RchlValidatorException {
		// // 验证必选字段
		// for (Map.Entry<String, Object> entry :
		// cvb.getRequiredValidate().entrySet()) {
		// Object reqData = entry.getValue();
		// String key = entry.getKey();
		// try {
		// requiredValidate((String) reqData);
		// } catch (RchlValidatorException e) {
		// e.getExceptionMessage().setDetailText(key + " 是必填项不能为空");
		// e.getExceptionMessage().setDetailText(key + " 是必填项不能为空");
		// throw e;
		// }
		// }
	}

	/**
	 * @Description (验证字段)
	 * @param cvb
	 *            验证信息
	 */
	public static void validate(RchlValidateBean cvb) throws RchlValidatorException {
		// 验证必选字段
		for (Map.Entry<String, Object> entry : cvb.getRequiredValidate().entrySet()) {
			Object reqData = entry.getValue();
			String key = entry.getKey();
			try {
				if (reqData == null) {
					throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
							new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(), "必填项为空",
									"必填项为空"));
				}
				requiredValidate(String.valueOf(reqData));
			} catch (RchlValidatorException e) {
				e.getExceptionMessage().setSimpleText(key + " 是必填项不能为空");
				e.getExceptionMessage().setDetailText(key + " 是必填项不能为空");
				throw e;
			}
		}

		// int 类型数据验证
		for (Map.Entry<String, Object> entry : cvb.getIntValidate().entrySet()) {
			String reqData = String.valueOf(entry.getValue());
			String key = entry.getKey();
			try {
				if (entry.getValue() == null) {
					throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
							new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(), null,
									null));
				}
				intValidate(reqData);
			} catch (RchlValidatorException e) {
				e.getExceptionMessage().setSimpleText(key + " 是数值型参数");
				e.getExceptionMessage().setDetailText(key + " 是必须为数值型,实际为:" + reqData);
				throw e;
			}
		}

		// long 类型数据验证
		for (Map.Entry<String, Object> entry : cvb.getLongValidate().entrySet()) {
			String reqData = (String) entry.getValue();
			String key = entry.getKey();
			try {
				longValidate(reqData);
			} catch (RchlValidatorException e) {
				e.getExceptionMessage().setSimpleText(key + " 是数值型参数");
				e.getExceptionMessage().setDetailText(key + " 是必须为数值型,实际为:" + reqData);
				throw e;
			}
		}

		// 手机号验证
		for (Map.Entry<String, Object> entry : cvb.getPhoneValidate().entrySet()) {
			String reqData = (String) entry.getValue();
			String key = entry.getKey();
			try {
				phoneValidate(reqData);
			} catch (RchlValidatorException e) {
				e.getExceptionMessage().setSimpleText(key + " 是手机号类型参数");
				e.getExceptionMessage().setDetailText(key + " 是机号类型参数,实际值为:" + reqData);
				throw e;
			}
		}

		// 验证字符串是否符合 json格式
		for (Map.Entry<String, String> entry : cvb.getJsonObjValidate().entrySet()) {
			String reqData = entry.getValue();
			String key = entry.getKey();
			try {
				jsonObjValidate(reqData);
			} catch (RchlValidatorException e) {
				e.getExceptionMessage().setSimpleText(key + " 必须是JSON格式");
				e.getExceptionMessage().setDetailText(key + " 必须是JSON格式,实际值为:" + reqData);
				throw e;
			}
		}


	}

	
	public static void validate(JSONObject obj ,List<String> requiredKeys) throws RchlValidatorException {
		
		for(String key:requiredKeys){
			
			if(!obj.containsKey(key)){
				throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
						new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(), "json 属属性填项不能为空",
								obj+",属性"+key+" 必填项为空"));
			}
		}
	}
	
	/**
	 * @Title: requiredValidate @Description: TODO(必填项验证) @param @param
	 *         data @param @throws RchlValidatorException 参数 @return void
	 *         返回类型 @throws
	 */
	private static void requiredValidate(String data) throws RchlValidatorException {
		if (null == data || "".equals(data)) {
			throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
					new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(), "必填项为空",
							"必填项为空"));
		}
	}

	/**
	 * @Title: intValidate @Description: TODO(int 型参数验证) @param @param
	 *         data @param @throws RchlValidatorException 参数 @return void
	 *         返回类型 @throws
	 */
	private static void intValidate(String data) throws RchlValidatorException {
		if (StringUtils.isBlank(data) || !GenericValidator.isInt((String) data)) {
			throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
					new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(), null, null));

		}

	}

	/**
	 * @Title: LongValidate @Description: TODO(long 类型验证) @param @param
	 *         data @param @throws RchlValidatorException 参数 @return void
	 *         返回类型 @throws
	 */
	private static void longValidate(String data) throws RchlValidatorException {
		if (StringUtils.isBlank(data) || !GenericValidator.isLong(data)) {
			throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
					new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(), null, null));

		}
	}

	/**
	 * @Title: phoneValidate @Description: TODO(手机号验证) @param @param
	 *         data @param @throws RchlValidatorException 参数 @return void
	 *         返回类型 @throws
	 */
	private static void phoneValidate(String data) throws RchlValidatorException {
		if (StringUtils.isBlank(data) || !GenericValidator.isLong(data)) {
			throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
					new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(), null, null));

		}
	}

	private static void jsonObjValidate(String data) throws RchlValidatorException {
		try {
			JSONObject.parseObject(data);
		} catch (Exception e) {
			throw new RchlValidatorException(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getId(), null,
					new ExceptionMessage(RchlValidatorException.TypeEnum.EXCEPTION_VALIDATOR.getDesc(), null, null));
		}
	}
}
